package com.gsoe.auth.controller.admin;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.gsoe.auth.dto.UserDTO;
import com.gsoe.auth.dto.UserDisableDTO;
import com.gsoe.auth.dto.UserPermissionDTO;
import com.gsoe.auth.dto.UserRoleDTO;
import com.gsoe.auth.vo.RolePermissionVO;
import com.gsoe.auth.vo.RoleVO;
import com.gsoe.auth.vo.UserRoleVO;
import com.gsoe.auth.vo.UserVO;
import com.gsoe.auth.service.PowerService;
import com.gsoe.auth.service.RoleService;
import com.gsoe.auth.service.UserInfoService;
import com.gsoe.base.model.PageResult;
import com.gsoe.base.model.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")

public class UserAdminController {
    @Autowired
    UserInfoService userInfoService;

    @Autowired
    RoleService roleService;

    @Autowired
    PowerService powerService;

    @PostMapping("/getUserList")
    public Result<PageResult<UserVO>> getUserList(@RequestBody UserDTO userDTO) {
        return Result.success(userInfoService.getUserList(userDTO));
    }

    @DeleteMapping("/delUser/{id}")
    @SaCheckPermission("del_user")
    public Result<String> delUser(@PathVariable Long id) {
        userInfoService.delUser(id);
        return Result.success("ok");
    }

    @GetMapping("/getRoleListByid/{id}")
    public Result<UserRoleVO> getRoleListByid(@PathVariable Long id) {
        return userInfoService.getRoleListByid(id);
    }

    @PostMapping("/saveRoleList")
    @SaCheckPermission("save_user_role")
    public Result<String> saveRoleList(@RequestBody UserRoleDTO dto) {
        userInfoService.saveRoleList(dto);
        return Result.success("ok");
    }

    @PostMapping("/disable")
    @SaCheckPermission("user_disable")
    public  Result<String>userDisable(@RequestBody UserDisableDTO userDisableDTO){
        userInfoService.userDisable(userDisableDTO);
        return Result.success("ok");
    }

    @PostMapping("/saveUserPermission")
    @SaCheckPermission("save_user_role")
    public Result<String> saveUserPermission(@RequestBody UserPermissionDTO userPermissionDTO){
        userInfoService.saveUserPermission(userPermissionDTO);
        return Result.success("ok");
    }

    @GetMapping("/getRolePermission/{roleId}")
    @SaCheckPermission("save_user_role")
    public Result<RolePermissionVO> getRolePermission(@PathVariable("roleId") Long roleId){
        return Result.success(powerService.getRolePermission(roleId));
    }

    @GetMapping("/getRoleList")
    @SaCheckPermission("save_user_role")
    public Result<List<RoleVO>> getRoleList(){
        return Result.success(roleService.list().stream().map(r->{
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(r,roleVO);
            return roleVO;
        }).toList());
    }

    @DeleteMapping("/delRole/{roleId}")
    @SaCheckPermission("save_user_role")
    public Result<String> delRole(@PathVariable("roleId") Long roleId){
        roleService.delRole(roleId);
        return Result.success("ok");
    }
}
